package Netease;

import java.util.Scanner;

/**
 * @author Jimmy Zhan WORKSTATION
 * @since 2023/8/20 14:42
 * 说明：封闭岛屿的数量
 */

public class NeteaseClosedIsland {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String[] rowsStr = input.split("\\],\\[");
        int rows = rowsStr.length;
        int cols = rowsStr[0].split(",").length;

        int[][] grid = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            String[] values = rowsStr[i].replaceAll("\\[|\\]", "").split(",");
            for (int j = 0; j < cols; j++) {
                grid[i][j] = Integer.parseInt(values[j]);
            }
        }
        System.out.println(closedIsland(grid));
    }

    public static int closedIsland (int[][] grid) {
        int ans = 0;
        int m = grid.length;
        int n = grid[0].length;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0 && dfs(i, j, grid, m, n)) {
                    ans++;
                }
            }
        }
        return ans;
    }

    public static boolean dfs(int x, int y, int[][] grid, int m, int n) {
        // 终止条件：
        // 1. x，y越界
        // 2.
        // 3.
        // 4.
        //
        if (x < 0 || y < 0 || x >= m || y >= n) {
            return false;
        }
        if (grid[x][y] != 0) {  // 陆地
            return true;
        }
        grid[x][y] = -1;
        boolean left = dfs(x-1, y, grid, m, n);
        boolean right = dfs(x+1, y, grid, m, n);
        boolean top = dfs(x, y-1, grid, m, n);
        boolean bottom = dfs(x, y+1, grid, m, n);
        return left && right && top && bottom;
    }
}
